/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nl.hva.web.mvcwebapp.doa;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import nl.hva.web.mvcwebapp.models.Assignment;
import nl.hva.web.mvcwebapp.models.AssignmentAnswer;
import nl.hva.web.mvcwebapp.models.Chapter;
import nl.hva.web.mvcwebapp.models.User;
import nl.hva.web.mvcwebapp.models.Course;
import nl.hva.web.mvcwebapp.service.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

/**
 *
 * @author Thijs
 */
public class AssignmentDAO {

    public AssignmentDAO() {
    }

    public static List<Assignment> getAllAssignments(int chapter_id) {

        AnnotationConfiguration config = new AnnotationConfiguration();
        config.addAnnotatedClass(Assignment.class);
        SessionFactory factory = config.configure().buildSessionFactory();
        Session session = factory.getCurrentSession();
        session.beginTransaction();
        Query queryResult = session.createQuery("FROM Assignment WHERE chapter = '" + chapter_id + "'");

        List<Assignment> assignment_list = queryResult.list();
//        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
//        Transaction tx = session.beginTransaction();
//        List<Chapter> chapter_list = session.createCriteria(Chapter.class).list();
//        tx.commit();

        return assignment_list;
    }

    public static Assignment getAssignment(int assignment_id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        Assignment assignment = (Assignment) session.get(Assignment.class, assignment_id);
        tx.commit();

        //Course course = new Course(1, 1, "Java", "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendiere ante, id porttitor ipsum semper non. Etiam mollis euismod dapibus. Phasellus vel turpis sem. Vestibulum dui ...", "1", "http://www.downloadstad.nl/screenshot/Java.jpg");
        return assignment;
    }

    public static ArrayList insertAssignment(HttpServletRequest request, Chapter chapter) {
        ArrayList validation_errors = new ArrayList();
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        Assignment assignment = getAssignmentFromRequest(request, chapter);
        Assignment assignmentwithanswers = addAnswersForAssignment(request, assignment);
        session.save(assignmentwithanswers);
        tx.commit();
        return validation_errors;
    }

    public static Assignment addAnswersForAssignment(HttpServletRequest request, Assignment assignment) {
        String[] answers = request.getParameterValues("answers");
        String[] checks = null;
        if (request.getParameterValues("checkoption") != null) {
            System.out.println("Yes checks");
            checks = request.getParameterValues("checkoption");
        } else if (request.getParameterValues("radiooption") != null) {
            System.out.println("Yes radio");
            checks = request.getParameterValues("radiooption");
        } else {
            System.out.println("Nothing");
        }
        for (int j = 0; j < answers.length; j++) {
            int rightanswer = 0;
            for (int c = 0; c < checks.length; c++) {
                if(checks[c].equals(answers[j])){
                    rightanswer = 1;
                }
            }
            System.out.println("AnswerName: "+answers[j]+ ", answer right: "+rightanswer+"");
            AssignmentAnswer assignmentanswer = new AssignmentAnswer(answers[j], rightanswer);
            assignment.addAssignmentsanswer(assignmentanswer);
        }
        return assignment;
    }

    public static ArrayList updateAssignment(HttpServletRequest request, Chapter chapter) {
        ArrayList validation_errors = new ArrayList();
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        Assignment assignment = getAssignmentFromRequest(request, chapter);
        session.update(assignment);
        tx.commit();
        return validation_errors;
    }

    public static void deleteAssignment(int assignment_id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        Assignment tempAssignment = (Assignment) session.load(Assignment.class, assignment_id);
        session.delete(tempAssignment);
        tx.commit();
    }

    private static Assignment getAssignmentFromRequest(HttpServletRequest request, Chapter chapter) {
        Assignment a = new Assignment();

        if (request.getParameter("assignmentid") != null && !request.getParameter("assignmentid").isEmpty()) {
            a.setAssignmentid(Integer.parseInt(request.getParameter("assignmentid")));
        }
        if (request.getParameter("assignmenttype") != null && !request.getParameter("assignmenttype").isEmpty()) {
            a.setAssignmenttype(Integer.parseInt(request.getParameter("assignmenttype")));
        }
        if (request.getParameter("assignmentname") != null && !request.getParameter("assignmentname").isEmpty()) {
            a.setAssignmentname(request.getParameter("assignmentname"));
        }
        if (request.getParameter("assignmentquestion") != null && !request.getParameter("assignmentquestion").isEmpty()) {
            a.setAssignmentquestion(request.getParameter("assignmentquestion"));
        }
        if (chapter != null) {
            a.setChapter(chapter);
        }
        return a;
    }
}
